class Solution {  
public:  
    ListNode* reverseBetween(ListNode* head, int left, int right) {  
        if (head == nullptr || left == right) {  
            return head;  
        }  
          
        ListNode* prev = nullptr;  
        ListNode* curr = head;  
        ListNode* next = nullptr;  
        int count = 1;  
          
        // 移动 prev 指针到第 left-1 个节点  
        while (count < left) {  
            prev = curr;  
            curr = curr->next;  
            count++;  
        }  
          
        // 记录当前节点的下一个节点，以便后续反转链表时使用  
        next = curr->next;  
          
        // 移动 curr 指针到第 right 个节点  
        while (count <= right) {  
            ListNode* temp = curr->next;  
            curr->next = temp->next;  
            temp->next = prev->next;  
            prev->next = temp;  
            count++;  
        }  
          
        // 返回反转后的链表头节点  
        return next;  
    }  
};